package ywh.ui
{
	import fl.controls.Slider;
	import fl.core.InvalidationType;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class SliderForContent extends Slider
	{
		protected var _bgPadding:Number = 0;
		protected var _tbgPadding:Number;
		protected var _bgIsOut:Boolean=true;
		protected var trackBGLeft:Sprite;
		protected var trackBGLeftMask:Sprite;		
		protected var trackBGRight:Sprite;
		protected var trackBGWidth:Number;
		protected var trackBGRightMask:Sprite;
		protected static var defaultStyles:Object = {
				thumbUpSkin: "content_SliderThumb", 
				thumbOverSkin: "content_SliderThumb",
				thumbDownSkin: "content_SliderThumb", 
				thumbDisabledSkin: "content_SliderThumb",
				sliderTrackSkin: "content_SliderTrack_skin",
				sliderTrackDisabledSkin: "content_SliderTrack_skin",
				trackBackgournd: "content_SliderTrack_bg", 
				tickSkin: null, 
				focusRectSkin: null, 
				focusRectPadding: null
			}
		
		public static function getStyleDefinition():Object
		{
			return defaultStyles;
		}
		
		public function SliderForContent()
		{
			super();
			liveDragging = true;
		}
		
		override protected function configUI():void
		{
			super.configUI();
			var thumbOne:DisplayObject = getDisplayObjectInstance(getStyleValue("thumbUpSkin"));
			thumb.setSize(thumbOne.width, thumbOne.height);			
			var trackOne:DisplayObject = getDisplayObjectInstance(getStyleValue("sliderTrackSkin"));
			track.height = trackOne.height;
			drawTrackBackGround(trackOne.height);
			
		}
		
		private function drawTrackBackGround(height:Number):void
		{
			trackBGLeft = getDisplayObjectInstance(getStyleValue("trackBackgournd")) as Sprite;
			trackBGLeft.addEventListener(MouseEvent.MOUSE_DOWN, trackBG_mouseDown);
			trackBGLeftMask = new Sprite();
			trackBGLeftMask.graphics.beginFill(0x00FF00);
			trackBGLeftMask.graphics.drawRect(0, 0, 3, height);
			trackBGLeftMask.x = 0;
			trackBGLeft.addChild(trackBGLeftMask);
			trackBGLeft.mask = trackBGLeftMask;
			addChildAt(trackBGLeft, 0);
			
			trackBGRight = getDisplayObjectInstance(getStyleValue("trackBackgournd")) as Sprite;
			trackBGRight.addEventListener(MouseEvent.MOUSE_DOWN, trackBG_mouseDown);
			trackBGWidth = trackBGRight.width;
			trackBGRightMask = new Sprite();
			trackBGRightMask.graphics.beginFill(0xFF0000);
			trackBGRightMask.graphics.drawRect(0, 0, 3, height);
			trackBGRight.addChild(trackBGRightMask);
			trackBGRight.mask = trackBGRightMask;
			addChildAt(trackBGRight, 0);
		
		}
		
		private function trackBG_mouseDown(e:MouseEvent):void
		{
			switch (e.target) {
				case  trackBGLeft:
					value = minimum;
					break;
				case 	trackBGRight:
					value = maximum;
				default:
					break;
			}		
		}
		
		override protected function draw():void
		{
			if (isInvalid("bg") || isInvalid(InvalidationType.ALL))
			{
				_tbgPadding = bgIsOut?(_bgPadding + thumb.width / 2):_bgPadding;
				trackBGLeft.x = -_tbgPadding;
				trackBGLeftMask.width = trackBGRightMask.width = _tbgPadding;
				trackBGRightMask.x = trackBGWidth - _tbgPadding;
			}
			if (isInvalid("bg") || isInvalid(InvalidationType.SIZE))
			{
				trackBGRight.x = width+_tbgPadding-trackBGWidth;
			}
			super.draw();
		}
		
		[Inspectable(defaultValue=0,name=边界值)]
		
		public function get bgPadding():Number
		{
			return _bgPadding;
		}
		
		public function set bgPadding(value:Number):void
		{
			_bgPadding = value;
			invalidate("bg");
		}
		
		[Inspectable(defaultValue=true,name=包含滑块)]
		public function get bgIsOut():Boolean
		{
			return _bgIsOut;
		}		
		public function set bgIsOut(value:Boolean):void
		{
			_bgIsOut = value;
			invalidate("bg");
		}
	}

}